package com.cloudspace.jindun.adapter.locate;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cloudspace.jindun.JindunApp;
import com.cloudspace.jindun.R;
import com.cloudspace.jindun.model.LocatePerson;
import com.cloudspace.jindun.utils.ObjUtil;

import java.util.List;

/**
 * Created by zengxianhua on 17/3/4.
 */

public class LocateAreaPersonsAdapter extends BaseAdapter{
    private List<LocatePerson> datas;

    public void setDatas(List<LocatePerson> datas){
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (ObjUtil.isEmpty(datas)){
            return 0;
        }

        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        if (ObjUtil.isEmpty(datas)){
            return null;
        }

        if (position < 0 || position >= datas.size()){
            return null;
        }

        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    private int itemTextColors[] = {
            R.color.color_333333,
            R.color.red
    };

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null){
            convertView = View.inflate(JindunApp.getInstance(), R.layout.locate_statistics_persons_item, null);

            holder = new ViewHolder();
            holder.nameTv = (TextView) convertView.findViewById(R.id.name_tv);
            holder.areaTv = (TextView) convertView.findViewById(R.id.area_tv);
            holder.statusTv = (TextView) convertView.findViewById(R.id.status_tv);
            holder.timeTv = (TextView) convertView.findViewById(R.id.time_tv);

            convertView.setTag(holder);
        }

        holder = (ViewHolder) convertView.getTag();
        LocatePerson data = datas.get(position);

        int color = itemTextColors[0];
        if ("0001".equals(data.userStatus)){
            //安全
            holder.statusTv.setText(JindunApp.getInstance().getResources().getString(R.string.locate_safe));
        }else{
            holder.statusTv.setText(JindunApp.getInstance().getResources().getString(R.string.locate_loss));
            color = itemTextColors[1];
        }

        holder.nameTv.setText(data.userName);

        if (!TextUtils.isEmpty(departName)){
            if (data.person != null){
                holder.areaTv.setText(departName);
            }
        }else{
            holder.areaTv.setText(data.areaName);
        }
        holder.timeTv.setText(data.userOnlineDateValue);

        holder.nameTv.setTextColor(JindunApp.getInstance().getResources().getColor(color));
        holder.areaTv.setTextColor(JindunApp.getInstance().getResources().getColor(color));
        holder.timeTv.setTextColor(JindunApp.getInstance().getResources().getColor(color));
        holder.statusTv.setTextColor(JindunApp.getInstance().getResources().getColor(color));

        return convertView;
    }

    private String departName;

    public void setDepartName(String departName) {
        this.departName = departName;
    }

    static class ViewHolder{
        TextView nameTv;
        TextView areaTv;
        TextView statusTv;
        TextView timeTv;
    }
}


